Can someone explain to me why the answer is 3cos3x (it has something to do with chain rule i think)

Use the fundamental theorem of calculus. It says that
[tex]\displaystyle\frac{\mathrm d}{\mathrm dx}\int_{\text{constant}}^{g(x)}f(t)\,\mathrm dt=f(g(x))\cdot\dfrac{\mathrm dg}{\mathrm dx}[/tex]
In this case, [tex]g(x)=3x[/tex] and [tex]f(t)=\cos t[/tex]. Then
[tex]\displaystyle\frac{\mathrm d}{\mathrm dx}\int_0^{3x}\cos t\,\mathrm dt=\cos(3x)\cdot\frac{\mathrm d}{\mathrm dx}3x=3\cos3x[/tex]