Ball joints are used to compensate for slight differences in height. These can have topographical or thermal causes. Thermal influences in particular are dynamic. Ball joints form a relief point here and prevent emerging stresses in the pipeline.
If a pipeline has to be opened at a certain point for inspection purposes, ball joints allow it to be swung out so that access to the inside of the line is free.