As a general rule of working with electrical wires, you should only connect like to like. That means that black wires can be connected to black wires and blue to blue but usually, you do not want to connect two wires of a different color to each other because wires in different colors perform different functions. \
However, both black and red wires are live wires, or hot wires, which supply power to fixtures and outlets. You can connect these wires together but it is always advisable to connect wires only of the same color together and avoid mismatching colors even when wires perform the same function. \