Some trains can be connected to the grid 24/7 through overhead wires and/or onboard reserve batteries. This grid could be powered by greener sources of energy.
On a job, I went to site which services these things. Heart breakingly, after they have stripped down, serviced, and rebuilt the massive diesel engine, they run it, flat out, for 8 hours and all that energy, which could power a good chunk of the town, is thrown away as heat in a load bank. Plumes of dirty diesel exaughst are common on site.
What co2 emissions does a train produce per passenger km? Compared to a car?
That's also only when you don't have an electrified line. Electrification is more doable than one might think.
Example: the trans Siberian railway which traverses a huge and poor area has overhead power.