As I was going back home, I took the same train as that person by coincidence.
On my way home,I happened to be riding on the same train as that person で正しいです. ほかの言い方は:
As I was going back home, I took the same train as that person by coincidence:
”家に帰る途中で、私はその人と同じ電車を偶然に持って行きました” という意味です
by coincidence=偶然と