A crew member of a train has a very demanding job. The shifts are highly irregular and you may have to spend the night far from home. In this regard it's even worse than flying.
Someone has to maintain the infrastructure, which by its very nature can also be in remote places.
Particularly busy railroad crossings also require staffing, even if they're ostensibly in the middle of nowhere.
Very few people in our ageing societies want this sort of life and you have to incentivise them to do it, which adds costs.