The idea dates to the 1860s, when it was used as a plot device in a contemporary theatrical melodrama, though it owes much of its current recognition-value to its use in silent-era films. I know of at least half a dozen real-life examples, as well, several of which ended quite horribly.
A good brief look at your question was provided some months ago by u/Bodark43 in this earlier response:
Was getting tied to a railway a real thing?
If you scroll down, you will find links to further AskHistorians resources further down in the thread.
Here is the response from /u/EricHart when I asked this before