Maximizing custom likelihood

No this isn’t doing what you wrote above. Your code above is doing argmax_{(a, x)} log(p(\theta \ | \, x)p(x \ | \,a)p(a) ie maximizing the logp regarding all the input parameters.

In general, you need to find a way to approximate the integral, which is not an easy task. Numerical integration above is one way, otherwise, there are ways such as Laplace approximation, Monte Carlo simulation (writing the integral as an expectation) with tricks like important sampling etc.