> They are duly appointed and armed federal police officers that gather their authority in Canada via the Railway Safety Act as well as other acts.
Canadian Pacific is nearly 150 years old, I would bet that the Canadian government didn't want to be on the hook for policing such a massive geographic footprint for a private business, but at the same time did want to ensure that an industry that was vital to national security was secured. The alternative was probably nationalizing the entire business, which they likely also didn't have the stomach for.
For the rest of the continent: North America is commonly defined as the US, Canada and Mexico - Mexico's first rail system was established in 1837 sixteen years after independence in 1821 and the US's first railroad was built in 1827 just over fifty years after independence.
These places were very much the frontier when the railroads were built. There was minimal law enforcement provided by the barely-existent territorial governments and the railroads responded by hiring private security (famously Union Pacific hired Pinkerton) and building their own private law enforcement capacity and this was eventually formalized by laws in both countries permitting the practice of railroads having private police.
Much less familiar with the relevant Mexican history.
[1] https://youtu.be/A3eG8ZbeXeQ?t=167
[2] https://en.wikipedia.org/wiki/Canadian_Pacific_Kansas_City_P...