Idling has been the practice since the beginning of diesel locomotives on the railroad, not that it justifies it at all. Historically, when power is at the ready, it is started and running. The oil is warm and as soon as the crew walks in the door it is ready to be put on line and go... that's called expediting. This formula is accurate for fuel consumed at idle for a decent running EMD: 1 quart of diesel fuel per cylinder per hour. Idling excessively isn't healthy for an engine internally and causes excessive carbon buildup in the exhaust manifolds and lube oil to be burned (or unburned) and thrown out of the exhaust stacks all over the place and whatever is in the area. But then again, not too many locomotives idle all day and night nowadays, except in winter...
The first situation, without a doubt, is weather specific. Most modern locomotives are equipped with an automatic start/stop system that will shut down the locomotive and then restart it before critical minimum levels are reached - ie coolant temperature, air pressure, battery voltage levels etc. Older locomotives can be outfitted with a "hot start" system which accomplishes the same things, and are reasonably priced considering the fuel saved, I would imagine. Locomotives that are not equipped not only are very difficult to be started when ice cold, but leaving an engine with water coolant (because no railroad except short lines wish to put hundreds of gallons of high quality antifreeze in their locomotives) shut down for extended periods of time in sustained low temperatures below 35 degrees shatters pipes and cracks prime mover engine blocks. Most railroads have some sort of standard that calls for locomotives in yard and sometimes road service, situation dependent, to be shut down when they will not be used for over (approx) 45 minutes to 1 hour and ambient temperature is above 35 or 40 degrees. In the same way, if an engine dies with an ambient temperature below or expected to drop below 35 degrees and can't be restarted, it must be drained to avoid the damage described above. Get into really extreme low temperatures ie -10 to -30 F you'll find engines "idling" in Run 3 or 4 because they can't keep themselves warm while sitting.
Another situation - "pumping air" or keeping a train that has received an air test charged. Shut down locomotives attached to that train and eventually the air is going to bleed off. Let brake pipe pressure fall below 60psi and the 90 car air test your conductor just walked is void if left that way for over 4 hours. For example: the time elapsed between the minute you tied down a train after going dead and the time it takes the next crew to cab out to get the train. I was that second conductor many times... very annoying.
I can go on with the examples, but as you said it probably boils down to implemtentation costs for older locomotives versus the life they may have left in them on a particular railroad. As for the PA state law, I have no idea.