There are two different way CarPlay connects to your the infotainment system in your car: Wired (typically through USB port) and Wireless (uses Bluetooth to initiate connection and transfers data through WiFi). For folks with wired connection, I recommend making sure the Lightning connection port on your phone is free of lint or debris. It is common problem for it to fill up with lint over time, eventually causing connectivity issues for the lightning cable.
BMW only offers wireless CarPlay (at least in both of my 2018 BMW models). Wireless disconnections are different, and I haven't found a way to reliably address the issues I am having. I've tried the suggest remedy of switching into airplane mode via settings, and the switching back, rebooting phone, restarting car. It did work for a while but problem returned during a long drive. Still trying to debug this. Apple recommend returning phone to factory settings, and setting up as new phone. Then add apps as needed and note if adding/using particular app is associated with issue starting.
Steps for returning to factory settings and setting up as new phone:
back up your phone: https://support.apple.com/en-us/HT203977
restore to factory settings: https://support.apple.com/en-us/HT201252