I thought it worked that way... This is like my third iOS device (iPod touch 6th Gen, iPhone 7 Plus for like a month lol and now this 10.5 inch iPad)
I think the pencil disconnects when its been inactive and the iPad simultaneously goes to sleep for a relatively long period of time. This is done to save battery.
If you connect it to the charger, it remains connected for a while but will loose connection if you leave it there for a long time (say charging from 20-100%). You'll then have to plug it in