A few reasons.
In a city like Bombay, pretty much every square foot/mile/inch is covered with people walking to-from somewhere. The only place that is free of constant hordes of people at any given time are the railway tracks.
Train tracks in India are accessible from any point along the route (there are no fences, etc. to keep people out). Also, there’s no electrified third-rail.
Many of the slums in Bombay are along the rail route. So, for someone living in the slums by the railway track, the track itself, and its immediate surroundings, is the most accessible and relatively private space. Also, not too many people are likely to step on your droppings.
I’ll try to draw here a common configuration of tracks/slums/streets/apartments in Bombay:
============= - railway track
----------------------- - back-wall adjacent to track that separates the slums from the track
oooooooooooooo - slums along the track
////////////////////// - regular main road/street
************** - regular shops and residences/business etc. on the other side of the street from the slums
Note that the other side of the railway track can have the same config as this side, or it could be more industrial areas with no land available to squat (no pun intended) on.
There is another reason that I can think of, although I’m not sure if it’s significant. If you squat on the rail itself, you are raised from the ground and don’t need to adjust to keep from backup touching you (you really need to have camped out in the wilderness to understand this).
Interestingly, it’s not that no one sees them defecating. If you’re in a passing train, you’ll often see people defecating along the route, between the back-wall and the track as shown in Fig. A above.
Also interestingly, many trains in India have toilets that directly exit to the track, i.e. you sit, you drop, it falls onto the track below as the train chugs along. That’s why you’ll commonly see people in more rural areas or smaller cities stop theur vehical while a train goes over a bridge above them, lest they have some human droppings fall onto them (it happens).
Another very common space is the beach. People who live in slums along the coastline use the beach.