"Bach gave the title Das Wohltemperirte Clavier to a book of preludes and fugues in all 24 keys, major and minor, dated 1722,
composed "for the profit and use of musical youth desirous of learning, and especially for the pastime of those already skilled in this study"."
The purpose of the Art of the Fugue was most likely the same. Lots of 18th century composers employed by the church wrote versets (eg. look at
Pasterwitz) and other similar collections of contrapuntal pieces out of necessity (for educational and recreational purposes). That was part of their tradition of profession and craftsmanship. Also, due to the Baroque idiomatic use of rhythm and dynamics in Bach, it may sound to the modern ears like it's not for "entertainment", but the same can be said about just about anything Baroque; Purcell fantasies for viols and Biber sonatas. Once you understand how a fugue or a canon from those times works, there's nothing hard to "get" (I'm not implying Bach lacks inspiration or mastery, by this). I think it's nonsensical to think Bach somehow had an "avant-garde" mindset, actually intended to write things not for "entertainment". Bach himself in his time never actually thought in that way, just like how he thought the Doctrine of the Affections was always the way to compose music; he would have thought the music of the later eras with their mood swings (involving multiple themes), for instance, as lacking focus and confusing.