Why would evolution favor blue as a color in bugs?
Similarly, you could ask


Why are bees yellow?
Why are some frogs so colourful?
Why widowbirds have such long tails?
Why do peacocks have such a tail?


There are a number of reasons why such traits could evolve. The main reasons are:


Sexual selection (esp. intra-sexual selection when it comes to colors)
Mimicry (esp. batesian mimicry)
Aposematism


I let you investigate further by yourself through reading the linked wikipedia articles.

For the specific beetle, you have seen, it will be impossible to address the question unless we know the species.