Short answer: Yes, it's a regulatory requirement.
Longer answer: Rail transportation is tightly regulated everywhere in the world and, because of its very nature, the regulations are quite divergent from region to region (unlike road, air or sea transport). In the USA, the safety aspects of rail trasportation are regulated by the Federal Railroad Administration; and their regulations have influenced also Canada and Mexico. All peculiar features of North American trains, such as the characteristic horn sound, alternating headlights flashing when approaching a station or a level crossing (called "railroad crossing" or "grade crossing" in the USA), bells, etc., are regulated by the laws. Either the policymakers believed these were necessary for safety, or the railroad industry invented them and subsequently these features became fossilized in the regulations.
As a result, North American trains must sound their horns tons of times when approaching level crossings or stations at speed. When approaching slowly, they ring their bells and flash their headlights. In contrast, European trains sound their horns (very different sound) fewer times and (at least in countries I'm familiar with) only if the level crossing has no barriers and/or warning lights.
Subway trains, while also running on steel rails and thus very similar from the physical point of view, are a completely different universe from the operational and legal point of view. This is true not only in the USA, but in just about every country that has them. In France, for example, normal trains run on the left, while subways run on the right (except Lyon).