Welcome Vladimir_stefanov,
Thank you for using Apple Support Communities. Based on your post, when you AirPlay from the YouTube app and your iPhone locks, it stops the video playback. We'll do our best to assist with this.
Like your iPad, the iPhone being locked shouldn't disable or stop the playback. We use this feature often to AirPlay content from the YouTube app. With that said, we'd confirm you're AirPlaying this content according to: Use AirPlay to stream video or mirror the screen of your iPhone, iPad, or iPod touch
We'd also check and install updates that may be available for the iPhone and the YouTube app:
How to manually update apps on your Apple device
We would also like to mention not using a VPN or other security software on the iPhone, as they may cause issues with the connection.
Hoping to hear how it goes, take care.