This happens because of iOS/iPadOS Automatic AirPlay switching. Even if you have the main AirPlay automatic switching toggle off, audio handoff between apps can still occur when one audio session ends. Here are the steps to minimize or stop this:
1. Go to Settings > AirPlay & Continuity (or AirPlay & Handoff depending on your iPadOS version) and make sure Automatically AirPlay is set to Never. This is the primary setting.
2. When you pause Apple Music, tap the AirPlay icon in the Now Playing widget (or Control Center) and manually switch the output back to iPhone/iPad before opening YouTube. This prevents the system from inheriting the last active AirPlay destination.
3. In the YouTube app, before playing, tap the AirPlay/Cast icon in the video player and explicitly select iPad as the output. This locks YouTube's audio route to your device.
4. As a workaround, fully close Apple Music from the App Switcher before using YouTube, rather than just pausing it. This prevents any audio session handoff.
Unfortunately, there is no single toggle that completely prevents cross-app AirPlay inheritance in all situations, but the steps above should significantly reduce how often it happens.