The chemical reactions and pathways involving a hydrocarbon, a compound consisting of carbon and hydrogen only.