Whenever the adventure writer decides so
First off, it is up to you if you roll hp for a monster, or use the average number that is provided for simplicity and convenience (just as you can roll for damage or use the average provided). However, there are cases where monsters are given an explicit hp number that differs from the average.
Below are some examples from published adventures where the hit points deviate from the expected average. You can have higher hp, lower hp, even hp exceeding the normal maximum and additional abilities. When is that commonly done?
It seems to be pretty common to give major leaders or powerful individuals maximum or near maximum hit points. This makes them last longer so they will deliver a more memorable fight, and does not cause overhead like adding abilities, although for some leaders this is also done, making them even more impressive. In Tomb of Annihilation we get:
- A pterafolk leader with 40 hp instead of 26, near the maximum 44
- An aarakocra leader with 31 hp instead of 13 and even above the maximum 24, higher intelligence, wisdom, additional skills and spellcasting like a 5th level caster (which is enough to increase the CR to 2)
- A giant snapping turtle called King Toba with the maximum 120 hit points instead of 75, and advantage on saves against spells and magical effects
- A tyrannosaurus rex called the King of Feathers with 200 hit points instead of 136, near the maximal 208, and with other special traits such as seeing invisible creatures and legendary resistance
- The Wild Beyond the Witchlight has a 40 x 8 foot rug of smothering with the maximum 60 hp instead of 33.
It also is common to give lower hit points to weak opponents, for example very young or old ones that are infirm. In Tomb of Annihilation we get:
- Gondolo, a scout with 13 hp (instead of 16), portrayed as an incompetent fortune hunter
- A fledgling axe beak with 6 hp (instead of 19) and no effective attacks
- Elderly pterafolk with 13 hp (instead of 26)
Lastly, when the size of creature changes, then following page 7 of the Monster Manual their hit dice change, and this can affect hit points even when they are the average for the new adjusted size. In The Wild Beyond the Witchlight, we get:
- Sowpig, a small ghoul with 17 (5d6) hit points instead of 22 (5d8)
- Small animated armor with 27 (6d6+8) hp instead of 33 (6d8+6)
The DMG also provides this advice on page 273 about modifying existing monster stat blocks:
Adapting a stat block is far less time-consuming than
creating one from scratch, and there are changes you
can make to an existing monster that have no effect
on its challenge rating, such as swapping languages,
changing its alignment, or adding special senses.
However, once you change the creature's offensive or
defensive ability, such as its hit points or damage, its
challenge rating might need to change, as shown later
In most cases above where only the hit points were changed, the challenge rating was not changed, even for maximum hit points. For example, King Toba with its 120 hp has the same CR as a normal giant snapping turtle with 75, which normally would increase the defensive challenge rating from 1 to 4, and the overall challenge rating by at least one.
Given how inexact CRs are to begin with, it seems it is considered OK to simply keep the CR as is when picking a number like the maximum that is a possible outcome of a roll for the creature's hit dice. A CR adjustment is done in exceptional cases, like that of the aarakocra leader who also has multiple caster levels.