I think the clue to you question is the very first verse in the Chapter. The LORD was wrathful against Israel, and then used David to punish them. We know what the big sin of Israel was...running after the false gods of the Land they were inhabiting. Even David’s assistant knew that “Counting the Fighting Men” was a sin against Faith in God’s Provision and Guidance, and David soon realizes that HE is responsible for the sin of his people, since he is the Shepherd, and should have known better.
But, just think. If that whole debacle hadn’t occurred, David would never have seen the Angel up on the Temple Mount, and Solomon would never have built his Temple on that precise spot.
Thanks. The Lord used David, but I wondered what weird thing tripped him off to do that in the first place. It’s not like David was wanting to bring judgment down.
Maybe he was the type to obsess over numbers, and in his zeal got totally fixated on something and carried away. Yikes, I could see something like that happening!