The stock cooler design is based on the average TDP of that class of cpu. TDP is calculated at base speeds with average use apps, that means no turbo or boost or hyperthreading and no extreme apps like gaming. It's generally recommended the cooler be capable of at least 1.5x the cpu TDP, with a preference for 2x TDP if you want silent ish performance at gaming use.
The i5-8400 is a 65w TDP cpu. It's best match is a decent 140w cooler, in the CM hyper212, Arctic esports 33/34 class of coolers. That'll keep gaming temps in the mid 50's usually.
The Intel stock cooler on that cpu is rated closer to @ 70w, it's good enough to keep average websurfing, Facebook or light gaming inside safe limits, but extreme usage like heavy gaming, rendering etc will put it to almost throttle/shutdown temps. Technically it passes, but realistically it sux, it's hot and loud and continuous extended periods of time at those temps will eventually shorten its useful lifespan.
Oh, and if you haven't already, go into bios and hit F5 to return bios back to factory optimized default settings (fix ram after). There's hidden settings in bios and sometimes they'll not change with a cpu swap. Resetting bios will optimize them for the i5-8400 and remove the prior i3-8100 settings.