The Byron Bay Railroad Company runs the world’s first 100% solar-powered train. It wouldn’t work everywhere - but in the bright sunshine of Australia, it might just be the right tool for the job.
More about the railroad: https://byronbaytrain.com.au/
The rail area gets vibrated by the wheels (which would shorten the solar panel lifetimes directly), any dust that the vibrations kick up would settle on the panel and reduce its efficiency, and anything dangling underneath the train would absolutely destroy the panels.
Besides, if you have the solar panels on the train track then it still needs to be wired to the train, which means you need an overhead wire and you might as well just put the panels next to the train track, where it’s quieter and less vibrated.